﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using He.Web.UI;
using He.Common;
using System.Collections.Generic;

namespace He.Web.manage.member
{
    public partial class edit : Manage
    {
        public string action = Enums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");

            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
                this.txtUserName.Enabled = false;
            }
            if (!IsPostBack)
            {
                ddlRankBind();
                rblStateBind();
                if (action == Enums.ActionEnum.Edit.ToString()) //修改
                {
                    chkLoginLevel("editmember");
                    ShowInfo();
                }
                else
                {
                    chkLoginLevel("addmember");
                }
            }
        }

        #region 绑定等级=================================
        private void ddlRankBind()
        {
            List<Model.member_rank> lst = new BLL.member_rank().GetModelList("id>0");
            ddlRankId.Items.Clear();
            ddlRankId.Items.Add(new ListItem("请选择会员等级", ""));
            foreach (Model.member_rank item in lst)
            {
                ddlRankId.Items.Add(new ListItem(item.title, item.id.ToString()));
            }
        }
        #endregion

        #region 绑定状态=================================
        private void rblStateBind()
        {
            rblStatus.DataSource = EnumHelper.GetList(typeof(Enums.MemberStatus));
            rblStatus.DataTextField = "Text";
            rblStatus.DataValueField = "Value";
            rblStatus.DataBind();
            rblStatus.Items[0].Selected = true;
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo()
        {
            Model.member model = new BLL.member().GetModel(id);
            ddlRankId.SelectedValue = model.rank_id.ToString();
            lbUserName.Text = model.user_name;
            lbThisTime.Text = model.this_time;
            lbThisIp.Text = model.this_ip;
            lbRegTime.Text = model.reg_time.ToString();
            txtEmail.Text = model.email;
            txtNickame.Text = model.nickname;
            rblSex.SelectedValue = model.sex;
            txtBirthday.Text = model.birthday;
            txtTel.Text = model.tel;
            txtPhone.Text = model.phone;
            txtQQ.Text = model.qq;
            txtAddress.Text = model.address;
            rblStatus.SelectedValue = model.status.ToString();
            txtRemark.Text = model.remark;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.member bll = new BLL.member();
            Model.member model = new Model.member();
            if (bll.Exists(txtUserName.Text.Trim()))
            {
                JscriptMsg(350, 230, "错误提示", "改用户名已被使用，请输入其他用户名！", "back", "Error");
                return false;
            }
            model.rank_id = int.Parse(ddlRankId.SelectedValue);
            model.user_name = txtUserName.Text.Trim();
            model.user_pwd = CryptoHelper.DESEncrypt(txtUserPwd.Text.Trim());
            model.email = txtEmail.Text;
            model.nickname = txtNickame.Text;
            model.sex = rblSex.SelectedValue;
            model.birthday = txtBirthday.Text;
            model.tel = txtTel.Text;
            model.phone = txtPhone.Text;
            model.qq = txtQQ.Text;
            model.address = txtAddress.Text;
            model.status = int.Parse(rblStatus.SelectedValue);
            model.reg_time = DateTime.Now.ToString();
            model.remark = txtRemark.Text.Trim();
            if (bll.Add(model) > 0)
            {
                SaveLogs(string.Format("[会员管理]增加内容:{0}", model.user_name));
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.member bll = new BLL.member();
            Model.member model = bll.GetModel(_id);
            model.rank_id = int.Parse(ddlRankId.SelectedValue);
            model.email = txtEmail.Text;
            model.nickname = txtNickame.Text;
            model.sex = rblSex.SelectedValue;
            model.birthday = txtBirthday.Text;
            model.tel = txtTel.Text;
            model.phone = txtPhone.Text;
            model.qq = txtQQ.Text;
            model.address = txtAddress.Text;
            model.status = int.Parse(rblStatus.SelectedValue);
            model.remark = txtRemark.Text.Trim();
            if (bll.Update(model))
            {
                SaveLogs(string.Format("[会员管理]修改内容:{0}", model.user_name));
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string url = CookieHelper.Get("LastListUrl");
            if (string.IsNullOrEmpty(url))
            {
                url = Request.RawUrl;
            }

            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("修改成功啦！", url);
            }
            else //添加
            {
                if (!DoAdd())
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("增加成功啦！", url);
            }
        }
    }
}
